package com.specdot.kraster.core;

public abstract class Screen {
	protected int height;
	protected int width;
	
	public Screen(int height, int width) {
		this.height = height;
		this.width = width;
	}

	public abstract void putPixel(int x, int y, float r, float g, float b, float a);
	public void putPixel(float u, float v, float r, float g, float b, float a) {
		putPixel(width * u, height * v, r, g, b, a);
	}
	
	public void drawLineUV(float u0, float v0, float u1, float v1, float r, float g, float b, float a) {
		drawLineRaw((int) (width * u0), (int) (height * v0), (int) (width * u1), (int) (height * v1), 
				 r, g, b, a);
	}
	
	public void drawLineRaw(int x0, int y0, int x1, int y1, float r, float g, float b, float a) {
		int dx = Math.abs(x1 - x0), dy = Math.abs(y1 - y0);
		
		int p = 2 * dy - dx;
		int doubleDy = 2 * dy, doubleDx = 2 * dx, doubleDyMinusDx = 2 * (dy - dx);
		int x, y;

		if (x0 > x1) {
			x = x1;
			y = y1;
			x1 = x0;
		}
		else {
			x = x0;
			y = y0;
		}
		putPixel(x, y, r, g, b, a);

		while (x < x1) {
			x++;
			if (p < 0) {
				p += doubleDy;
			}
			else {
				y++;
				p += doubleDyMinusDx;
			}
			putPixel(x, y, r, g, b, a);
		}
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}
	
	public float getTexelHeight() {
		return 1f / height;
	}
	
	public float getTexelWidth() {
		return 1f / width;
	}
}
